# -*- coding:utf-8 -*- 
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from view import *
from mywork import *
from model import *
from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
from SimpleXMLRPCServer import SimpleXMLRPCServer
import base64,md5
def savepost(g_name,g_title,g_photo_descrip,g_photo_url,g_tag,g_catalog,g_id_group):
    id_group=g_id_group
    photo_descrip   =base64.b64decode(g_photo_descrip)
    photo_url       =base64.b64decode(g_photo_url)
    try:
        data=entry.all().filter('id_group =',int(base64.b64decode(id_group))).get()
        data.name
    except:
        data=entry()
        data.name            =code_data(base64.b64decode(g_name))
        data.title           =code_data(base64.b64decode(g_title))
            
        #pub_data        =db.DateTimeProperty(auto_now_add=True)
        data.tag             =code_data(base64.b64decode(g_tag))
        data.catalog         =code_data(base64.b64decode(g_catalog))
        #data.name_group      =base64.b64decode(g_name_group)
        data.id_group        =int(base64.b64decode(g_id_group))
        data.put()
    photo_group(photo_entry=data,
                photo_descrip=code_data(photo_descrip),
                photo_url=code_data(photo_url),).put()
    return 'post data ok'
    
def testapi():
    c="yes api is readly"
    return  c


##------------------------------------------------------------------------------
class PlogXMLRPCDispatcher(SimpleXMLRPCDispatcher):
    def __init__(self, funcs):
        SimpleXMLRPCDispatcher.__init__(self, True, 'utf-8')
        self.funcs = funcs

dispatcher = PlogXMLRPCDispatcher({'savepost':savepost,
                                   'testapi':testapi,})
class callapi(webapp.RequestHandler):
    def get(self):
        #Logger(request = self.request.uri, response = '----------------------------------').put()
        self.response.out.write('<h1>please use POST</h1>')
    def post(self):
        request = self.request.body
        response = dispatcher._marshaled_dispatch(request)
        self.response.out.write(response)


class MainPage(webapp.RequestHandler):
    
    
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp World!')


application = webapp.WSGIApplication([('/pic/.*',index),
                                      ('/up',updata),
				      ('/tag/.*',tag),
				      ('/catalog/.*',catalog),
                                      ('/test',test),
                                      ('/rpc',callapi),
				      ('/rss',feed),
                                      ('/getnowid',getnowid),
                                      ('/.*', mian_page),
                                      ], debug=True)


def main():
    run_wsgi_app(application)
